package com.jzo2o.es.config;

import lombok.Data;
import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@Data
@ConfigurationProperties(prefix = "jzo2o.es")
public class EsConfiguration {

    /**
     * es host
     */
    private String host;
    /**
     * es 端口
     */
    private Integer port;

    @Bean
    public RestHighLevelClient client(){
        return new RestHighLevelClient(RestClient.builder(
                new HttpHost(
                        host,
                        port,
                        "http"
                )
        ));
    }

//    private static final ObjectMapper MAPPER = new ObjectMapper();
//
//    static {
//        MAPPER.setPropertyNamingStrategy(PropertyNamingStrategies.SNAKE_CASE);
//        MAPPER.setSerializationInclusion(JsonInclude.Include.NON_NULL);
//
//        JavaTimeModule javaTimeModule = new JavaTimeModule();
//        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(DatePattern.NORM_DATETIME_PATTERN);
//        javaTimeModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(dateTimeFormatter));
//        javaTimeModule.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(dateTimeFormatter));
//        MAPPER.registerModule(javaTimeModule);
//
//    }
//
//    @Bean
//    public ElasticsearchClient esClient(EsProperties esProperties) {
//        RestClient restClient = RestClient.builder(new HttpHost(esProperties.getHost(), esProperties.getPort())).build();
//        // Create the transport with a Jackson mapper
//        // 使用自定义json序列化
//        JacksonJsonpMapper jacksonJsonpMapper = new JacksonJsonpMapper(MAPPER);
//
//        ElasticsearchTransport transport = new RestClientTransport(restClient, jacksonJsonpMapper);
//        // And create the API client
//        return new ElasticsearchClient(transport);
//    }
//
//    @Bean
//    public ElasticSearchTemplate template(ElasticsearchClient elasticsearchClient) {
//        return new ElasticSearchTemplateImpl(elasticsearchClient);
//    }
}
